import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

import Layout from "@/layout";

// 导入动态路由
import tiku from "./modules/tiku";
import mianshi from "./modules/mianshi";
import qiye from "./modules/qiye";
import xueke from "./modules/xueke";

// 静态路由
export const constantRoutes = [
  {
    path: "/login",
    component: () => import("@/views/login/index"),
    hidden: true,
  },

  {
    path: "/404",
    component: () => import("@/views/404"),
    hidden: true,
  },

  {
    path: "/",
    component: Layout,
    redirect: "/dashboard",
    children: [
      {
        path: "dashboard",
        name: "dashboard",
        component: () => import("@/views/dashboard/index"),
        meta: { title: "面板", icon: "dashboard" },
      },
    ],
  },

  {
    path: "/base",
    component: Layout,
    meta: { title: "后台管理", icon: "dashboard" },
    children: [
      {
        path: "base",
        name: "Base",
        component: () => import("@/views/base/index"),
      },
      {
        path: "users",
        name: "Users",
        component: () => import("@/views/base/users"),
        meta: { title: "用户", icon: "dashboard" },
      },
      {
        path: "menus",
        name: "Menus",
        component: () => import("@/views/base/menus"),
        meta: { title: "菜单", icon: "dashboard" },
      },
      {
        path: "permissions",
        name: "Permissions",
        component: () => import("@/views/base/permissions"),
        meta: { title: "权限", icon: "dashboard" },
      },
      {
        path: "logs",
        name: "Logs",
        component: () => import("@/views/base/logs"),
        meta: { title: "日志", icon: "dashboard" },
      },
    ],
  },
  // 将来404 这里要注释掉，要放在路由配置的最后
  { path: "*", redirect: "/404", hidden: true },
];

// 动态路由
export const asyncRoutes = [qiye, tiku, xueke, mianshi];

// 创建路由实例的函数
const createRouter = () =>
  new Router({
    mode: "hash",
    scrollBehavior: () => ({ y: 0 }),
    // TODO: 没做权限之间
    routes: [...constantRoutes, ...asyncRoutes],
    // TODO: 做了权限之后
    // routes: [...constantRoutes]
  });
// 创建路由实例
const router = createRouter();
// 重置路由实例
export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}
// 暴露路由实例
export default router;
